package com.ztej.query.activity;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.ztej.query.R;
import com.ztej.query.adapter.StaffAdapter;
import com.ztej.query.model.ContactInfo;
import com.ztej.query.parser.JsonHelper;
import com.ztej.query.utils.Tools;

import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StaffInfoActivity extends Activity {

    private Button btnBack;
    private TextView tvName, tvRegion, tvSex, tvAge, tvRole, tvDuty;
    private ImageView ivPhoto;

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.icon_stub)
            .showImageForEmptyUri(R.drawable.icon_empty)
            .showImageOnFail(R.drawable.icon_error)
            .cacheInMemory()
            .cacheOnDisc()
            .displayer(new RoundedBitmapDisplayer(5))
//            .decodingOptions(decodingOptions)
            .imageScaleType(ImageScaleType.NONE)
            .build();

//    Map<ContactInfo> mapContact = new HashMap<String, String>();

//    List<Map<String, String>> listContact = new ArrayList<Map<String, String>>();
    List<ContactInfo> list_contactInfo = new ArrayList<ContactInfo>();
    private static class MyHandler extends Handler {
        private final WeakReference<StaffInfoActivity> mActivity;

        public MyHandler(StaffInfoActivity activity) {
            mActivity = new WeakReference<StaffInfoActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            StaffInfoActivity activity = mActivity.get();
            if (activity != null) {
                // ...
                switch (msg.what){
                    case 0:
                        JsonHelper jsonHelper = new JsonHelper();
                        activity.list_contactInfo = jsonHelper.parseContactInfo((String)msg.obj);
                        activity.tvName.setText(activity.list_contactInfo.get(0).getName());
                        activity.tvRegion.setText(activity.list_contactInfo.get(0).getRegion());
                        ImageLoader.getInstance().displayImage(activity.list_contactInfo.get(0).getPhotourl(), activity.ivPhoto, activity.options);
                        activity.tvSex.setText(activity.list_contactInfo.get(0).getSex());
                        activity.tvAge.setText(activity.list_contactInfo.get(0).getAge());
                        activity.tvRole.setText(activity.list_contactInfo.get(0).getRole());
                        activity.tvDuty.setText(activity.list_contactInfo.get(0).getDuty());
                        break;
                }
            }
        }
    }
    private final StaffInfoActivity.MyHandler mHandler = new StaffInfoActivity.MyHandler(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_staff_info);
        initViews();
        if (Tools.isNetworkAvailable(this)){
            getContactInfo(getIntent().getStringExtra("id"));
        } else {
            Toast.makeText(this, "当前没有可用的网络", Toast.LENGTH_SHORT).show();
        }

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });



    }

    private void initViews(){
        btnBack = (Button)findViewById(R.id.btnBack);
        tvName = (TextView) findViewById(R.id.tvName);
        tvRegion = (TextView) findViewById(R.id.tvRegion);
        ivPhoto = (ImageView)findViewById(R.id.ivPhoto);
        tvSex = (TextView) findViewById(R.id.tvSex);
        tvAge = (TextView) findViewById(R.id.tvAge);
        tvRole = (TextView) findViewById(R.id.tvRole);
        tvDuty = (TextView) findViewById(R.id.tvDuty);

    }

    private void getContactInfo(final String id){
        new Thread(){
            public void run(){

                try {
                    URL url = new URL("http://hr.hrqylm.com/app/hr/User.aspx?uid=" + id);
//                    String content = "";
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setReadTimeout(5000);
                    // 设置允许输出
                    conn.setDoOutput(true);
                    conn.setRequestMethod("GET");
                    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    OutputStream os = conn.getOutputStream();
//                    os.write(content.getBytes("UTF-8"));
                    os.close();
                    //服务器返回的响应码
                    if (conn.getResponseCode() == 200){
                        InputStream is = conn.getInputStream();
                        String text = Tools.getTextFromStream(is);
                        System.out.println("---------------------------" + text);
                        Message msg = new Message();
                        msg.what = 0;
                        msg.obj = text;
                        mHandler.sendMessage(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }



}
